﻿Option Strict On
Option Explicit On

Imports System.Security.Cryptography
Imports Microsoft.Win32
Imports System.Text

Public Class ConexionDB


    Public Shared str_aplicacion As String = "SOFTWARE\Serigraphica"
    Private Shared key_conn As String = "UKFast Calvin Murshed _ FaSHoNe PopupResizeMode Holi XD T_T 8798"

    Public Shared ReadOnly Property BD_ConexionSistema() As String
        Get
            Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey(str_aplicacion, False)
            Return DecryptConnection(CType(key.GetValue("BDCONNECTION", ""), String), True)
        End Get
    End Property

    Public Shared Function DecryptConnection(ByVal cipherString As String, ByVal useHashing As Boolean) As String
        Dim keyArray As Byte()
        Dim toEncryptArray As Byte() = Convert.FromBase64String(cipherString)

        'Dim settingsReader As System.Configuration.AppSettingsReader = New AppSettingsReader()
        'Get your key from config file to open the lock!
        'Dim key As String = DirectCast(settingsReader.GetValue("SecurityKey", GetType([String])), String)
        'Dim key As String = "Syed Moshiur Murshed _ lallal %# Gato Holi >.<"

        If useHashing Then
            Dim hashmd5 As New MD5CryptoServiceProvider()
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key_conn))
            hashmd5.Clear()
        Else
            keyArray = UTF8Encoding.UTF8.GetBytes(key_conn)
        End If

        Dim tdes As New TripleDESCryptoServiceProvider()
        tdes.Key = keyArray
        tdes.Mode = CipherMode.ECB
        tdes.Padding = PaddingMode.PKCS7

        Dim cTransform As ICryptoTransform = tdes.CreateDecryptor()
        Dim resultArray As Byte() = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length)

        tdes.Clear()
        Return UTF8Encoding.UTF8.GetString(resultArray)
    End Function
End Class
